#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/6 下午4:01
# @Author  : xiaomao
# @Site    : 
# @File    : views.py
# @Software: PyCharm

from django.contrib.contenttypes.models import ContentType
from django.http import JsonResponse
from django.db.models import ObjectDoesNotExist
from .models import LikeCount, LikeRecord
from django.views import View
from common.utils import get_logger
from django.contrib.auth.mixins import LoginRequiredMixin

logger = get_logger(__name__)


def ErrorResponse(code, message):
    data = {}
    data['status'] = 'ERROR'
    data['code'] = code
    data['message'] = message
    return JsonResponse(data)

def SuccessResponse(liked_num):
    data = {}
    data['status'] = 'SUCCESS'
    data['liked_num'] = liked_num
    return JsonResponse(data)


class LikeChange(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
    # 获取数据
        user = request.user
        # print(user)
        if not user.is_authenticated:
            return ErrorResponse(400, 'you were not login')

        content_type = request.GET.get('content_type')
        object_id = request.GET.get('object_id')

        try:
            content_type = ContentType.objects.get(model=content_type)
            model_class = content_type.model_class()
            model_obj = model_class.objects.get(pk=object_id)
        except ObjectDoesNotExist:
            return ErrorResponse(401, 'object not exist')

        # 处理数据
        if request.GET.get('is_like') == 'true':
            # 要点赞
            like_record, created = LikeRecord.objects.get_or_create(content_type=content_type, object_id=object_id, user=user)
            if created:
                # 未点赞过，进行点赞
                like_count, created = LikeCount.objects.get_or_create(content_type=content_type, object_id=object_id)
                like_count.liked_num += 1
                like_count.save()
                return SuccessResponse(like_count.liked_num)
            else:
                # 已点赞过，不能重复点赞
                return ErrorResponse(402, 'you were liked')
        else:
            # 要取消点赞
            if LikeRecord.objects.filter(content_type=content_type, object_id=object_id, user=user).exists():
                # 有点赞过，取消点赞
                like_record = LikeRecord.objects.get(content_type=content_type, object_id=object_id, user=user)
                like_record.delete()
                # 点赞总数减1
                like_count, created = LikeCount.objects.get_or_create(content_type=content_type, object_id=object_id)
                if not created:
                    like_count.liked_num -= 1
                    like_count.save()
                    return SuccessResponse(like_count.liked_num)
                else:
                    return ErrorResponse(404, 'data error')
            else:
                # 没有点赞过，不能取消
                return ErrorResponse(403, 'you were not liked')


